﻿package com.fxd.geolocation
{
	import com.google.maps.LatLng;
	
	import flash.display.MovieClip;
	import flash.display.*;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.events.Event;
	import flash.display.StageScaleMode;
	import flash.events.GeolocationEvent;
	import flash.events.StatusEvent;
	import flash.sensors.Geolocation;
	import flash.text.Font;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.*;

	public class GPS extends Sprite
	{
		protected var geo:Geolocation;
		protected var base_time:int = getTimer();
		protected var lineNo:int=0;
		//protected var gps_mc:GPSClip;
		public var lat:Number = -100;
		public var lng:Number = 0;
		protected var map_mc:MovieClip;
		
		public function GPS()
		{
			//nearby(2.48291, 51.618017)
			/*gps_mc = new GPSClip();
			addChild(gps_mc);
			gps_mc.x = 45;
			gps_mc.y = 565;*/
			//alert("Just walk away.");

			if (Geolocation.isSupported) {
				//alert("Geolocation is supported");
				// muted would mean user didn't grant permission for read_geolocation
				
				//Initialize the location sensor if not done before
				geo = new Geolocation();
				if (geo.muted) {
					alert('Please enable GPS');
				}else{
					alert('Finding your current location');
				}
				geo.addEventListener(StatusEvent.STATUS, onStatusEvent);
				// muted would mean user didn't grant permission for read_geolocation
				geo.setRequestedUpdateInterval(2000);
				geo.addEventListener(GeolocationEvent.UPDATE, onGeolocationEvent);
				
				
			}else{
				alert('Geolocation not supported!');
				return;
			}
		}
		protected function onStatusEvent(e:StatusEvent):void {
			// remove the update event handler if it isn't needed anymore
			if (geo != null){
				if (geo.muted){
					geo.removeEventListener(GeolocationEvent.UPDATE, onStatusEvent);
				}else{
					geo.setRequestedUpdateInterval(2000);
					geo.addEventListener(GeolocationEvent.UPDATE, onGeolocationEvent);
				}
			}
			/*if (!geo.muted) {
				alert('Geolocation is not muted');
				// every 2 seconds should be plenty	
				//Register to receive location updates.
				// when unit goes to standby, gets STATUS event wiht Geolocation.Unmuted
				// and when out of standby, says Geolocation.Muted!
				
				
			}else{
				alert('Geolocation muted');
			}*/
			//alert("status Time: "+ getTimer() +'  Type: '+ e.type +'  Code: '+ e.code +'  Target: '+ e.target +'  Muted: '+ geo.muted);
		}
		
		protected var done_first:Boolean;
		protected function onGeolocationEvent(e:GeolocationEvent):void {
			alert('Found your location');
			
			// check for a garbage event and ignore it
			if (e.latitude == 0.0 && e.longitude == 0.0)
			{
				//alert(" Time: "+ getTimer() + '  False  TimeStamp: ' + e.timestamp.toFixed(6) +'  Type: '+ e.type);
				return;
			}
			if (!done_first) {
				done_first = true;
				//gps_mc.findSatellite();
				//alert(" Time: "+ getTimer() + 'FIRST  TimeStamp: '+ e.timestamp.toFixed(6) +'  Type: '+ e.type);
				if(map_mc){
					map_mc.showMe(lat,lng);
				}
			}
			
			//var msg:String = e.timestamp.toFixed(6) + ': (' +
			var msg:String = '(' +
				e.latitude.toFixed(6) + ', ' +
				e.longitude.toFixed(6) + ', ' +
				int(e.altitude).toString() + 'm)  / +/- ' +
				e.horizontalAccuracy.toFixed(1) + 'm/' +
				e.verticalAccuracy.toFixed(1) + 'm, ' +
				e.speed.toFixed(2) + 'm/s, ' +
				e.heading.toFixed(1) + '\u00b0';
			
			lat = Number(e.latitude.toFixed(6));
			lng = Number(e.longitude.toFixed(6));
			//alert(msg);
			dispatchEvent(new Event("LOCATION_FOUND"));
		}
		protected function alert(msg:String):void{
			/*lineNo++;
			var obj:Object = new Object();
			obj.No = lineNo;
			obj.Message = msg;*/
			
			trace("GPS : " + msg);
			//gps_mc.stateText(msg)
		}

		public function addMap(mc:MovieClip):void{
			map_mc = mc;
			map_mc.showMe(lat,lng);
		}
	}
}